package com.zzyl;


import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionChunk;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

import java.util.function.Consumer;


public class main {
    private static final String prompt = "你能帮我分析一份完整的体检报告吗？";
    public static void main(String[] args) {

        OpenAIClient client = OpenAIOkHttpClient.builder()
                .apiKey("bce-v3/ALTAK-3flUET4QVQQaJJZwoLtPu/bf4c55d3ccc51a0eea8338720155baff5a92d3b1") //将your_APIKey替换为真实值，如何获取API Key请查看https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Um2wxbaps#步骤二-获取api-key
                .baseUrl("https://qianfan.baidubce.com/v2/") //千帆ModelBuilder平台地址
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(prompt) // 对话messages信息
                .model("ernie-4.5-turbo-128k") // 模型对应的model值，请查看支持的模型列表：https://cloud.baidu.com/doc/WENXINWORKSHOP/s/wm7ltcvgc
                .build();

        // 非流式
//        ChatCompletion chatCompletion = client.chat().completions().create(params);
//        System.out.println(chatCompletion.choices().get(0).message().content());

        // 流式
        StreamResponse<ChatCompletionChunk> chatCompletion = client.chat().completions().createStreaming(params);
        Consumer<ChatCompletionChunk> consumer = s -> System.out.println(s.choices().get(0).delta().content());
        chatCompletion.stream().forEach(consumer);
    }
}